﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;

namespace Sweepids.Models.Dao
{
    public class Dao
    {
// ReSharper disable InconsistentNaming
        public OracleConnection myConnection;
// ReSharper restore InconsistentNaming

        public Dao()
        {
            myConnection = new OracleConnection
                {
                    ConnectionString =
                        "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=l-c30805)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl.stonehill.edu)));User Id=SYSTEM;" +
                        "Password = csrocks55;"
                };
        }

        public OracleDataReader RunCommand(OracleCommand myCommand, List<OracleParameter> paramList, bool isReaderCommand)
        {
            myCommand.BindByName = true;
            myCommand.CommandType = CommandType.Text;
            foreach (var param in paramList)
            {
                if (myCommand.CommandText.Contains(param.ParameterName))
                {
                    myCommand.Parameters.Add(param);
                }
            }
            try
            {
                if (isReaderCommand)
                {
                    myConnection.Open();
                    var result = myCommand.ExecuteReader();
                    return result;
                }
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                myConnection.Close();
                return null;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Dao::RunSqlCommand(SqlCommand, List<SqlParameter>, bool): " + e.Message);
                myConnection.Close();
                throw;
            }
        }

        public OracleDataReader RunCommand(OracleCommand myCommand, bool isReaderCommand)
        {
            return RunCommand(myCommand, new List<OracleParameter>(), isReaderCommand);
        }
    }
}